package com.aaa.controller;

import cn.hutool.db.Entity;
import com.aaa.service.PersonInfoServie;
import com.aaa.service.imp.PersonInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2024/7/15 15:10
 * @description：人员信息业务控制器
 * @modified By：
 * @version: 1.0
 */
@WebServlet("/person")
public class PersonInfoController extends HttpServlet {
    PersonInfoServie    personInfoServie = new PersonInfoServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求和响应的编码格式
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //获取请求参数，并且保存在map集合中
        Map map = new HashMap();
        Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameterName = parameterNames.nextElement();
            String parameterValue = req.getParameter(parameterName);
            map.put(parameterName, parameterValue);
        }
        //定义一个请求类别
        String type = req.getParameter("type");
        //对请求进行分类
        switch (type) {
            case "add":
                //保存数据到personinfo表中
                if (personInfoServie.addPersonInfo(map)) {
                    //保存成功，重新查询数据
                    findAll(req, resp);

                } else {
                    //保存失败
                    resp.getWriter().write("保存失败");
                }
                break;
            case "toAddPage":
                findAllArea( req, resp);
                //转发到页面addPerson.jsp
                req.getRequestDispatcher("addPerson.jsp").forward(req, resp);
                break;
            case "toUpdatePage":
                findAllArea( req, resp);
                //根据personid查询personinfo表中的一行数据
                Entity personInfo = personInfoServie.findPersonInfoById(map);
                //将查询结果保存到session中
                req.getSession().setAttribute("person", personInfo);
                //转发到页面updatePerson.jsp
                req.getRequestDispatcher("updatePerson.jsp").forward(req, resp);
            case "update":
                if (personInfoServie.updatePersonInfo(map)) {
                    //修改成功，重新查询数据
                    findAll(req, resp);
                } else {
                    //修改失败
                    resp.getWriter().write("修改失败");
                }
                break;
            case "delete":
                if (personInfoServie.deletePersonInfo(map)) {
                    //删除成功，重新查询数据
                    findAll(req, resp);
                } else {
                    //删除失败
                    resp.getWriter().write("删除失败");
                }
                break;
            case "findById":
                break;
            case "findAll":
                findAll(req, resp);
                break;
                default:
                    resp.getWriter().write("请求类型错误");
                    break;

        }
    }
    /**
     * @create by: Teacher陈
     * @description: 查询所有人员信息
     * @create time: 2024/7/15 16:17
     * @param req
     * @param resp
     * @return void
     */
    protected  void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Entity> personList = personInfoServie.findPersonInfo();
        //将查询结果保存到session中
        req.getSession().setAttribute("personList", personList);
        //转发到页面showPerson.jsp
        req.getRequestDispatcher("showPerson.jsp").forward(req, resp);
    }
    protected  void findAllArea(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //查询所有的区域信息保存到areaList
        List<Entity> areaList = personInfoServie.findAreaInfo();
        //将查询结果保存到session中
        req.getSession().setAttribute("areaList", areaList);
    }
}
